/**
 *
 * Copyright Pact Lab of H.I.T.
 *
 * Designed and Implemented by Grid Researching Group, 
 * Pact Lab, Harbin
 * 
 * This Project is part of the national 973 Project:
 * Internet Based Virtual Computing Environment
 *
 * http://pact518.hit.edu.cn
 * 
 * Author:       Meteor <meteorlxk@gmail.com> 
 * Copyright:    pact518 
 * Version:      1.0
 * Created:      2009-4-20 
 * LastModified: 2010-5-20
 */
package edu.hit.pact.pgse.master;

import java.io.FileNotFoundException;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.util.zip.DataFormatException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import edu.hit.pact.pgse.ch.ConsistentHash;
import edu.hit.pact.pgse.ch.HashFunction;
import edu.hit.pact.pgse.ch.KetamaHashFunction;
import edu.hit.pact.pgse.communication.CommUtil;
import edu.hit.pact.pgse.master.handler.MasterClientHandler;
import edu.hit.pact.pgse.master.handler.MasterCrawlerHandler;
import edu.hit.pact.pgse.util.Globals;
import edu.hit.pact.plot.PlotProcessor;

/**
 * @author Meteor
 *
 */
public class MasterMainControl {
	
	public static void main(String args[]) throws FileNotFoundException, DataFormatException, MalformedURLException {
		System.out.println("");
		System.out.println("--------------------< P A C T   C R A W L E R >--------------------");
		System.out.println("");
		System.out.println("                    Master begins to work. ^-^                 ");
		System.out.println("");
		System.out.println("--------------------<   G O O D     L U C K   >--------------------");
		System.out.println("");
		System.out.println("");

		/*
		 * load log4j property.
		 */
		Globals.readLog4jProperties();

		Log logger = LogFactory.getLog(MasterMainControl.class);
		/*
		 * Construct consistent hash
		 */
		HashFunction function = new KetamaHashFunction();
		int numberOfReplicas = Globals.CONSISTENT_HASH_NUMBER_OF_REPLICAS;
		ConsistentHash<InetAddress> consistentHash = new ConsistentHash<InetAddress>(function, numberOfReplicas, logger);
		
		/*
		 * Construct master
		 */
		MasterManager masterManager = new MasterManager(consistentHash);
		CommUtil commUtil = new CommUtil();
		
		MasterCrawlerHandler masterCrawlerHandler = new MasterCrawlerHandler(masterManager);
		
		commUtil.setAcceptor(new MasterClientHandler(masterManager), Globals.MASTER_LISTEN_PORT_FOR_CLIENT);
		commUtil.setAcceptor(masterCrawlerHandler, Globals.MASTER_LISTEN_PORT_FOR_CRAWLER);
		
	}
}
